using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using twistmachinev2.Entity;

namespace twistmachinev2.CommonUse
{
    /// <summary>
    /// 用户权限管理辅助类
    /// </summary>
    public static class UserPermissionHelper
    {
        /// <summary>
        /// 权限级别枚举
        /// </summary>
        public enum PermissionLevel
        {
            /// <summary>
            /// 操作员权限
            /// </summary>
            Operator = 30,
            
            /// <summary>
            /// 管理员权限
            /// </summary>
            Manager = 60,
            
            /// <summary>
            /// 超级管理员权限
            /// </summary>
            SuperAdmin = 90
        }

        /// <summary>
        /// 检查当前用户是否有指定权限
        /// </summary>
        /// <param name="requiredPermission">所需权限级别</param>
        /// <returns>是否有权限</returns>
        public static bool HasPermission(PermissionLevel requiredPermission)
        {
            if (!BaseData.IsUserLoggedIn)
            {
                return false;
            }

            return BaseData.CurrentUserPermission >= (long)requiredPermission;
        }

        /// <summary>
        /// 检查当前用户是否为操作员
        /// </summary>
        /// <returns>是否为操作员</returns>
        public static bool IsOperator()
        {
            return HasPermission(PermissionLevel.Operator);
        }

        /// <summary>
        /// 检查当前用户是否为管理员
        /// </summary>
        /// <returns>是否为管理员</returns>
        public static bool IsManager()
        {
            return HasPermission(PermissionLevel.Manager);
        }

        /// <summary>
        /// 检查当前用户是否为超级管理员
        /// </summary>
        /// <returns>是否为超级管理员</returns>
        public static bool IsSuperAdmin()
        {
            return HasPermission(PermissionLevel.SuperAdmin);
        }

        /// <summary>
        /// 获取权限级别名称
        /// </summary>
        /// <param name="permissionValue">权限值</param>
        /// <returns>权限名称</returns>
        public static string GetPermissionName(long permissionValue)
        {
            switch (permissionValue)
            {
                case 30:
                    return "操作员";
                case 60:
                    return "管理员";
                case 90:
                    return "超级管理员";
                default:
                    return "未知权限";
            }
        }

        /// <summary>
        /// 获取当前用户权限名称
        /// </summary>
        /// <returns>当前用户权限名称</returns>
        public static string GetCurrentUserPermissionName()
        {
            if (!BaseData.IsUserLoggedIn)
            {
                return "未登录";
            }

            return GetPermissionName(BaseData.CurrentUserPermission);
        }

        /// <summary>
        /// 检查是否可以访问用户管理功能
        /// </summary>
        /// <returns>是否可以访问</returns>
        public static bool CanAccessUserManagement()
        {
            return HasPermission(PermissionLevel.Manager);
        }

        /// <summary>
        /// 检查是否可以访问系统设置功能
        /// </summary>
        /// <returns>是否可以访问</returns>
        public static bool CanAccessSystemSettings()
        {
            return HasPermission(PermissionLevel.SuperAdmin);
        }

        /// <summary>
        /// 检查是否可以执行设备操作
        /// </summary>
        /// <returns>是否可以执行</returns>
        public static bool CanOperateDevice()
        {
            return HasPermission(PermissionLevel.Operator);
        }

        /// <summary>
        /// 验证用户权限并显示错误信息
        /// </summary>
        /// <param name="requiredPermission">所需权限级别</param>
        /// <param name="operationName">操作名称</param>
        /// <returns>是否有权限</returns>
        public static bool ValidatePermissionWithMessage(PermissionLevel requiredPermission, string operationName = "此操作")
        {
            if (!BaseData.IsUserLoggedIn)
            {
                DevExpress.XtraEditors.XtraMessageBox.Show("请先登录系统！", "权限验证", 
                    System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
                return false;
            }

            if (!HasPermission(requiredPermission))
            {
                DevExpress.XtraEditors.XtraMessageBox.Show(
                    $"{operationName}需要{GetPermissionName((long)requiredPermission)}权限！\n\n当前用户权限：{GetCurrentUserPermissionName()}", 
                    "权限不足", 
                    System.Windows.Forms.MessageBoxButtons.OK, 
                    System.Windows.Forms.MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 获取用户状态显示文本
        /// </summary>
        /// <returns>用户状态文本</returns>
        public static string GetUserStatusText()
        {
            if (!BaseData.IsUserLoggedIn)
            {
                return "未登录";
            }

            return $"{BaseData.CurrentUserName} ({GetCurrentUserPermissionName()})";
        }
    }
}